# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/3/1
# @File   : EC04_ExtGuarBalToCapitalPro.py
# @E-mail : zhaochen@bbdservice.com


'''eof
name:对外担保余额(总)比注册资金
code:EC04_ExtGuarBalToCapitalPro
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ,IC_DSJ,EG_DSJ
type:常用指标
dataSourceType:在线指标 description:这是获取 EC04 字段的数据脚本
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTableEC(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def getTableIC(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def getTableEG(table_Name):
    try:
        data = EG_DSJ['data']['R1103V3'][0].get(table_Name)
        if data in null_type_list:
            return None
        else:
            if isinstance(data, list) and len(data) >= 1:
                return data
            else:
                return [{}]
    except:
        return None

def isfloat(x):
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False
#注册资本
def EGCapital():
    try:
        basic = getTableEG('basicList')
        if basic is None or basic == [{}]:
            return None
        else:
            if basic[0].get('regCap') not in null_type_list and isfloat(basic[0].get('regCap')):
                regCap = float(basic[0].get('regCap'))
                return regCap
            else:
                return None
    except:
        return None

# 个人对外担保
def ICGuarantee():
    try:
        ICRGuaranteeSummary = getTableIC('ICRGuaranteeSummary')
        if ICRGuaranteeSummary is None:
            return None
        elif ICRGuaranteeSummary == [{}]:
            return 0.0
        else:
            if ICRGuaranteeSummary[0].get('reportNo') not in null_type_list \
                and isfloat(ICRGuaranteeSummary[0].get('balance')):
                return float(ICRGuaranteeSummary[0].get('balance'))
            else:
                return None
    except:
        return None


#企业对外担保
def ECGuarantee():
    try:
        ECRSummaryGuarantee = getTableEC('ECRSummaryGuarantee')
        if ECRSummaryGuarantee is None:
            return None
        elif ECRSummaryGuarantee == [{}]:
            return 0.0
        else:
            flag = []
            bz_sum = 0
            dy_sum = 0
            zy_sum = 0
            for i in ECRSummaryGuarantee:
                count_flag = 0
                if i.get('Guaranteetype') == u'保证汇总' and i.get('ReportNo') not in null_type_list:
                    normal = i.get('GuaranteedBalance_Normal')
                    concered = i.get('GuaranteedBalance_Concerned')
                    bad = i.get('GuaranteedBalance_Bad')
                    try:
                        normal = float(normal)
                    except:
                        normal = 0
                        count_flag += 1
                    try:
                        concered = float(concered)
                    except:
                        concered = 0
                        count_flag += 1
                    try:
                        bad = float(bad)
                    except:
                        bad = 0
                        count_flag += 1
                    if count_flag == 3:
                        bz_sum = None
                    else:
                        bz_sum = normal + concered + bad

                elif i.get('Guaranteetype') == u'抵押汇总' and i.get('ReportNo') not in null_type_list:
                    normal = i.get('GuaranteedBalance_Normal')
                    concered = i.get('GuaranteedBalance_Concerned')
                    bad = i.get('GuaranteedBalance_Bad')
                    try:
                        normal = float(normal)
                    except:
                        normal = 0
                        count_flag += 1
                    try:
                        concered = float(concered)
                    except:
                        concered = 0
                        count_flag += 1
                    try:
                        bad = float(bad)
                    except:
                        bad = 0
                        count_flag += 1
                    if count_flag == 3:
                        dy_sum = None
                    else:
                        dy_sum = normal + concered + bad

                elif i.get('Guaranteetype') == u'质押汇总' and i.get('ReportNo') not in null_type_list:
                    normal = i.get('GuaranteedBalance_Normal')
                    concered = i.get('GuaranteedBalance_Concerned')
                    bad = i.get('GuaranteedBalance_Bad')
                    try:
                        normal = float(normal)
                    except:
                        normal = 0
                        count_flag += 1
                    try:
                        concered = float(concered)
                    except:
                        concered = 0
                        count_flag += 1
                    try:
                        bad = float(bad)
                    except:
                        bad = 0
                        count_flag += 1
                    if count_flag == 3:
                        zy_sum = None
                    else:
                        zy_sum = normal + concered + bad
                else:
                    flag.append(None)

            if dy_sum == None and zy_sum == None and bz_sum == None:
                return None
            elif len(flag) == len(ECRSummaryGuarantee):
                return None
            else:
                if dy_sum is None:
                    dy_sum = 0
                if zy_sum is None:
                    zy_sum = 0
                if bz_sum is None:
                    bz_sum = 0
                return dy_sum + zy_sum + bz_sum
    except:
        return None


def EC04_ExtGuarBalToCapitalPro():
    try:
        IC_guarantee = ICGuarantee()
        EC_guarantee = ECGuarantee()
        EG_capital = EGCapital()

        if EG_capital is None or EG_capital == 0:
            return u'缺失值'
        else:
            if IC_guarantee is None and EC_guarantee is None:
                return u'缺失值'
            else:
                if IC_guarantee is None:
                    IC_guarantee = 0.0
                else:
                    IC_guarantee = float(IC_guarantee) / 10000.0

                if EC_guarantee is None:
                    EC_guarantee = 0.0

                result = round((IC_guarantee + EC_guarantee) / float(EG_capital), 4)
                return result
    except:
        return u'缺失值'

result = EC04_ExtGuarBalToCapitalPro()